/**
 * UI组件类
 */
class UIBase extends eui.Component implements IView{
	/**
	 * 进入界面传入数据
	 */
	protected data: any;
	protected _rectBg:eui.Rect;

	public constructor(uiName?:string, data?:any) {
		super();

		if(uiName){
			this.skinName = uiName;
		}

		if(data){
			this.data = data;
		}
	}

	protected childrenCreated(){
		super.childrenCreated();

		this.showBG();
		this.init();
		this.addEvent();
		this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoveToStageHandler, this);
	}

	private onRemoveToStageHandler(){
		this.destroy();
	}

	/**
	 * 界面加载完成后的初始化
	 */
	public init(){

	}

	public showBG(){
		if(this.data){
			if(this.data['showBG']){
				if(!this._rectBg){
					this._rectBg = new eui.Rect(GlobalManager.instance().curWidth(), GlobalManager.instance().curHeight());
					
					this._rectBg.alpha = 0;
				}
				if(!this._rectBg.parent){
					this.addChildAt(this._rectBg, 0);
				}
			}
		}
	}

	public hideBG(){
		if(this._rectBg){
			if(this._rectBg.parent){
				this._rectBg.parent.removeChild(this._rectBg);
			}
		}
	}

	/**
	 * 添加监听
	 */
	protected addEvent(){
		if(this._rectBg){
			this._rectBg.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closeUI, this);
		}
	}

	/**
	 * 移除监听
	 */
	protected removeEvent(){
		if(this._rectBg){
			this._rectBg.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.closeUI, this);
		}
	}

	/**
	 * 清除数据，移除舞台时会自动调用
	 */
	protected clear(){
		this.hideBG();
		this.data = null;
		this._rectBg = null;
	}

	/**
	 * 更新数据
	 */
	public refresh(data:any){
		this.data = data;
	}

	/**
	 * 移除父容器显示列表
	 */
	public remove(effect?:number){
		if(this.parent){
			UIManager.instance().closeUILogic(egret.getQualifiedClassName(this), effect);
		}
	}

	private closeUI()
	{
		this.remove(0);
	}

	/**
	 * 清楚所有数据和监听
	 */
	public destroy(){
		this.removeEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoveToStageHandler, this);
		this.removeEvent();
		this.clear();
	}
}

window['UIBase'] = UIBase;